package by.grsu.epam.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import by.grsu.epam.domain.user.impl.Subscriber;
import by.grsu.epam.service.entitiesservice.AccountService;
import by.grsu.epam.service.entitiesservice.SubscriberService;
import by.grsu.epam.web.workentities.ChangePassword;

@Controller
public class ProfileController {

	@Autowired
	private SubscriberService subscriberService;

	@Autowired
	private AccountService accountService;

	@RequestMapping(value = "/subscriber/profile", method = RequestMethod.GET)
	public ModelAndView profileView() {
		ModelAndView mav = new ModelAndView("/subscriber/profile");

		mav.addObject("activeMenu", "profile");

		mav.addObject("subscriber", subscriberService.findByUsername(getUsername()));

		return mav;
	}

	// Change profile info

	@RequestMapping(value = "/subscriber/profile/changeInfo", method = RequestMethod.GET)
	public ModelAndView profileInfo() {
		ModelAndView mav = new ModelAndView("/subscriber/profile/changeInfo");

		mav.addObject("activeMenu", "profile");

		mav.addObject("subscriber", subscriberService.findByUsername(getUsername()));

		return mav;
	}

	@RequestMapping(value = "/subscriber/profile/changeInfo", method = RequestMethod.POST)
	public ModelAndView profileInfoChange(@ModelAttribute(value = "subscriber") Subscriber subscriber, BindingResult result) {

		ModelAndView mav = new ModelAndView();

		if (result.hasErrors()) {
			mav.setViewName("redirect:/subscriber/profile");
		} else {
			mav.setViewName("redirect:/subscriber/profile");

			subscriber = subscriberService.changeSubscriberInfo(subscriber);
			subscriberService.update(subscriber);
		}

		return mav;
	}

	// Profile change password

	@RequestMapping(value = "/subscriber/profile/changePassword", method = RequestMethod.GET)
	public ModelAndView profilePass() {
		ModelAndView mav = new ModelAndView("/subscriber/profile/changePassword");

		mav.addObject("activeMenu", "profile");

		mav.addObject("changePass", new ChangePassword());
		return mav;
	}

	@RequestMapping(value = "/subscriber/profile/changePassword", method = RequestMethod.POST)
	public ModelAndView profilePassChange(@ModelAttribute("changePass") ChangePassword changePass, BindingResult result) {

		ModelAndView mav = new ModelAndView();

		if (result.hasErrors()) {
			mav.setViewName("redirect:/subscriber/profile");
		} else {
			mav.setViewName("redirect:/subscriber/profile");

			Subscriber subscriber = subscriberService.changeSubscriberPass(getUsername(), changePass);
			subscriberService.update(subscriber);
		}

		return mav;
	}

	private String getUsername() {
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

		String username;
		if (principal instanceof UserDetails) {
			username = ((UserDetails) principal).getUsername();
		} else {
			username = principal.toString();
		}
		return username;
	}

}
